package com.wrb.wechat.controller;

import com.wrb.wechat.service.WechatService;
import com.wrb.wechat.utils.CheckUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wrb
 * @date 2019/11/25 11:40
 */
@Slf4j
@RestController
@RequestMapping("/wechat")
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(tags = "微信回调")
public class WechatController {

	@Autowired
	private WechatService wechatService;

	@RequestMapping(value = "/callback", method = {RequestMethod.POST}, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "接收微信回调")
	public String get(
			HttpServletRequest request, HttpServletResponse response
	) {
		String responseMessage = wechatService.processRequest(request);
		return responseMessage;
	}

	@RequestMapping(value = "/callback", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "服务器配置检查")
	public String rollback(
			@RequestParam(value = "signature", required = false) String signature, @RequestParam String timestamp,
			@RequestParam String nonce, @RequestParam String echostr
	) {
		log.info("----------------开始处理微信发过来的消息------------------");
		if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
			System.out.println(echostr);
		}
		return echostr;
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	@ApiOperation(value = "测试检查")
	public String test(
	) {
		return "success";
	}

}
